package com.huarun.shiro;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.stereotype.Component;

import com.huarun.common.util.Constant;
import com.huarun.module.user.service.IUserService;

/**
 * @author hqy
 * @创建时间   2018-10-18 14:50
 * @Description 主要是对未登录的用户进行过滤
 */
@Component
public class LoginAuthorizationFilter extends FormAuthenticationFilter {
	@Resource
	private IUserService userService;

	/**
	 * 这个方法是判断是否未登录需要执行的方法
	 */
	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
		/**智慧运营管理平台是否登入标识*/
		String token = request.getParameter("token");
		/**face++是否登入标识*/
		String cookie = request.getParameter("cookie");
		/**只要有一个标识不为空，则判断该用户已登入*/
		if (StringUtils.isBlank(token) && StringUtils.isBlank(cookie)) {
			Map<String, Object> params = new HashMap<String, Object>();
			//请求face++或者智慧运营管理平台得到cookie或token
			params.put("appkey", Constant.MANAGE_APPKEY);
			params.put("secret", Constant.MANAGE_SECRET);
			try {
//				token = HttpUtil.doGet(Constant.MANAGE_GETTOKEN, params);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	
	public static void main(String[] args) {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("G0CvDz7oJn666");
        //要加密的数据（数据库的用户名或密码）
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("Root1234");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }

}
